package cn.hd.swaggerService.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * created by dsl on 2022/6/25
 * swagger2配置
 */
@Component
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {

    @Bean({"createAllRestApi"})
    public Docket createAllRestApi(){
        DocketBuilder docketBuilder = this.docketBuilder();
        ApiInfo apiInfo = docketBuilder.buildApiInfo("基础服务平台", "基础服务平台", "v1.0");
        return docketBuilder.createBaseDocket(apiInfo,"authorService","cn.hd.authorService");
    }

    @Bean
    public DocketBuilder docketBuilder() {
        return new DocketBuilder();
    }

    public class DocketBuilder {
        public DocketBuilder() {
        }

        public Docket createBaseDocket(ApiInfo apiInfo, String groupName,String basePackage) {
            return  new Docket(DocumentationType.SWAGGER_2).pathMapping("/")
                    .groupName(groupName)
                    .apiInfo(apiInfo)
                    // 设置哪些接口暴露给Swagger展示
                    .select()
                    // 扫描所有有注解的api，用这种方式更灵活
                    .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                    // 扫描指定包中的swagger注解
                    .apis(RequestHandlerSelectors.basePackage(basePackage))
                    // 扫描所有 .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.regex("(?!/ApiError.*).*"))
                    .paths(PathSelectors.any())
                    .build()
                    .globalOperationParameters(getParameterList())//设置全局请求头
                    // 支持的通讯协议集合
                    .protocols(newHashSet("https", "http"));
        }

        public ApiInfo buildApiInfo(String title, String description, String version) {
            return (new ApiInfoBuilder()).title(title).description(description).contact(new Contact("hd", "https://swagger.io/", "1711054839@qq.com")).version(version).build();
        }

        /**
         * 支持的通讯协议集合
         * @param type1
         * @param type2
         * @return
         */
        private Set<String> newHashSet(String type1, String type2){
            Set<String> set = new HashSet<>();
            set.add(type1);
            set.add(type2);
            return set;
        }

        /**
         * 添加header参数配置
         */
        private List<Parameter> getParameterList() {
            ParameterBuilder clientIdTicket = new ParameterBuilder();
            List<Parameter> pars = new ArrayList<Parameter>();
            clientIdTicket.name("token").description("token请求头")
                    .modelRef(new ModelRef("string"))
                    .parameterType("header")
                    .required(true).build(); //设置false，表示clientId参数 非必填,可传可不传！
            pars.add(clientIdTicket.build());
            return pars;
        }
    }
}
